package com.dhecp.project.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysUserAndRoleFieldStandard;
import com.dhecp.project.system.mapper.SysUserAndRoleFieldStandardMapper;
import com.dhecp.project.system.service.SysUserAndRoleFieldStandardService;

/**
 * 字段隐藏权限设置
 * @author DH-Xxj
 * @version 2021年9月8日
 */
@Service
public class SysUserAndRoleFieldStandardServiceImpl implements SysUserAndRoleFieldStandardService {
    
    @Autowired
    private SysUserAndRoleFieldStandardMapper sysUserAndRoleFieldStandardMapper;

	/**
	 * 查询指定用户或角色字段隐藏权限
	 * @param moduleTable 必要参数：功能唯一标识【针对全局（非指定功能）设置时默认值：ALL】
	 * @param pid 必要参数：用户编号或角色guid
	 * @param vtype 必要参数：用户或角色类型（user：用户，role：角色）
	 * @param fieldHidden 必要参数：权限类型（0：显示，1：隐藏）
	 * @return List - SysUserAndRoleFieldStandard
	 * @author DH-Xxj
	 */
    @Override
    public List<SysUserAndRoleFieldStandard> queryFieldStandard(Map<String,Object> params){
        if (params.get("pid") == null || params.get("pid") == ""){
        	params.put("pid", "pid");
        }
        if (params.get("vtype") == null || params.get("vtype") == ""){
        	params.put("vtype", "vtype");
        }
        if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
        	params.put("moduleTable", "moduleTable");
        }
        if (params.get("fieldHidden") == null || params.get("fieldHidden") == "" || !"1".equals(params.get("fieldHidden").toString())){
        	params.put("fieldHidden", 0);
        }
    	return sysUserAndRoleFieldStandardMapper.findFieldStandard(params);
    }

	/**
	 * 设置指定用户或角色字段隐藏权限
	 * @param moduleName  可选参数：功能名称
	 * @param moduleTable 必要参数：功能唯一标识【针对全局（非指定功能）设置时默认值：ALL】
	 * @param pid 必要参数：用户编号或角色guid
	 * @param vtype 必要参数：用户或角色类型（user：用户，role：角色）
	 * @param fieldHidden 必要参数：权限类型（0：显示，1：隐藏）
	 * @param infoItems 必要参数：用户及角色字段隐藏权限表对应实体类的json格式字符串（包含：fieldStandardId【全局变量编码】,fieldStandardName【全局变量名称】）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @Override
    public AjaxResult saveUserAndRoleFieldStandard(Map<String, Object> params){
        String pid = "";
        String vtype = "";
        String moduleTable = "ALL";
        String moduleName = "ALL";
        Boolean fieldHidden = true;
        if (params.get("pid") == null || params.get("pid") == ""){
        	return AjaxResult.error(MessageUtils.message("module.error.lack","用户编号或角色guid【pid】"));
        }else{
        	pid = params.get("pid").toString();
        }
        if (params.get("vtype") == null || params.get("vtype") == ""){
        	return AjaxResult.error(MessageUtils.message("module.error.lack","用户或角色类型【vtype】"));
        }else{
        	vtype = params.get("vtype").toString();
        }
        if (params.get("fieldHidden") != null && "0".equals(params.get("fieldHidden").toString())){
        	fieldHidden = false;
        }else{
        	params.put("fieldHidden", 1);
        }
        if (params.get("moduleTable") != null && params.get("moduleTable") != ""){
        	moduleTable = params.get("moduleTable").toString();
        }else{
        	params.put("moduleTable", "ALL");
        }
        if (params.get("moduleName") != null && params.get("moduleName") != ""){
        	moduleName = params.get("moduleName").toString();
        }else{
        	params.put("moduleName", "ALL");
        }
    	sysUserAndRoleFieldStandardMapper.deleteFieldStandard(params);
        
        List<SysUserAndRoleFieldStandard> sysUserAndRoleFieldStandardList = JSON.parseArray(params.get("infoItems").toString(), SysUserAndRoleFieldStandard.class);
        if (sysUserAndRoleFieldStandardList != null && sysUserAndRoleFieldStandardList.size() > 0) {
            for (SysUserAndRoleFieldStandard sysUserAndRoleFieldStandard : sysUserAndRoleFieldStandardList) {
                if (sysUserAndRoleFieldStandard != null){
                	sysUserAndRoleFieldStandard.setPid(pid);
                	sysUserAndRoleFieldStandard.setVtype(vtype);
                	sysUserAndRoleFieldStandard.setModuleTable(moduleTable);
                	sysUserAndRoleFieldStandard.setModuleName(moduleName);
                	sysUserAndRoleFieldStandard.setFieldHidden(fieldHidden);
                	sysUserAndRoleFieldStandard.setUpdateUser(ShiroUtils.getUserId());
                	sysUserAndRoleFieldStandard.setUpdateTime(new Date());
                	sysUserAndRoleFieldStandard.setCreateUser(ShiroUtils.getUserId());
                	sysUserAndRoleFieldStandard.setCreateTime(new Date());
                	sysUserAndRoleFieldStandardMapper.insert(sysUserAndRoleFieldStandard);
                }
            }
        }
        
    	return AjaxResult.success("操作成功");
    }
    
}
